package com.eshop.modules.mp.handler;

import me.chanjar.weixin.common.error.WxErrorException;
import com.eshop.modules.mp.domain.WechatReply;
import me.chanjar.weixin.mp.builder.outxml.TextBuilder;
import com.alibaba.fastjson.JSONObject;
import cn.hutool.core.util.ObjectUtil;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import java.util.Map;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import org.springframework.beans.factory.annotation.Autowired;
import com.eshop.modules.mp.service.WechatReplyService;
import org.springframework.stereotype.Component;

@Component
public class SubscribeHandler extends AbstractHandler
{
    @Autowired
    private WechatReplyService wechatReplyService;
    
    public WxMpXmlOutMessage handle(final WxMpXmlMessage wxMessage, final Map<String, Object> context, final WxMpService weixinService, final WxSessionManager sessionManager) throws WxErrorException {
        String str = "你好，欢迎关注yshop!";
        final WechatReply wechatReply = this.wechatReplyService.isExist("subscribe");
        if (!ObjectUtil.isNull((Object)wechatReply)) {
            str = JSONObject.parseObject(wechatReply.getData()).getString("content");
        }
        try {
            final WxMpXmlOutMessage msg = (WxMpXmlOutMessage)((TextBuilder)((TextBuilder)WxMpXmlOutMessage.TEXT().content(str).fromUser(wxMessage.getToUser())).toUser(wxMessage.getFromUser())).build();
            return msg;
        }
        catch (Exception e) {
            this.logger.error(e.getMessage(), (Throwable)e);
            return null;
        }
    }
}
